﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebForm_ManageRoad_ManageRoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindGV();
            //判斷是否有找到
            if (((Label)Master.FindControl("lblHeadText")) != null)
            {
                ((Label)Master.FindControl("lblHeadText")).Text = this.GetLocalResourceObject("Title.Text").ToString();
            }
        }
    }
    #region "FormView事件"
    protected void ddlRegionID_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindGV();
    }
    protected void ddlRegionID_DataBound(object sender, EventArgs e)
    {
        this.AddHeader((DropDownList)fv.FindControl("ddlRegionID"));
    }
    protected void fv_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlItem;
        ddlItem = (DropDownList)fv.FindControl("ddlRegionID");
        ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(SqlDataSource2.SelectParameters["Parent"].DefaultValue));

        this.BindGV();
        gv.DataBind();
    }
    #endregion

    #region "GridView事件"

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)   
        {
            Label lblNO = (Label)e.Row.FindControl("lblNO");
            lblNO.Text = (gv.PageIndex * gv.PageSize + e.Row.RowIndex + 1).ToString();

            if ((DropDownList)e.Row.FindControl("ddlRegionID_Edit") != null)
            {
                DropDownList ddlItem;
                ddlItem = (DropDownList)e.Row.FindControl("ddlRegionID_Edit");
                ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(((HiddenField)e.Row.FindControl("hideRegionID")).Value));
            }
            LinkButton lbtnRoadItemName = (LinkButton)e.Row.FindControl("lbtnRoadItemName");
            if (lbtnRoadItemName!=null)
            {
                lbtnRoadItemName.Attributes["onclick"] = "OpenFile('" + gv.DataKeys[e.Row.RowIndex].Value.ToString() + "'," + DateTime.Now.ToString("hhmmss") + ");return false;";
            }
        }   
    }
    protected void ddlRegionID_Edit_DataBound(object sender, EventArgs e)
    {
        this.AddHeader((DropDownList)sender);
    }
    protected void ddlRegionID_Edit_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvRow = (GridViewRow)((DropDownList)sender).Parent.Parent;
        ((HiddenField)gvRow.FindControl("hideRegionID")).Value = ((DropDownList)sender).SelectedValue;
    }
    #endregion

    protected void BindGV()
    {
        if (((DropDownList)fv.FindControl("ddlRegionID")) != null)
        {
            SqlDataSource2.SelectParameters["Parent"].DefaultValue = ((DropDownList)fv.FindControl("ddlRegionID")).SelectedValue;
            SqlDataSource2.InsertParameters["Parent"].DefaultValue = ((DropDownList)fv.FindControl("ddlRegionID")).SelectedValue;
        }
    }
    protected void AddHeader(DropDownList ddlItem)
    {
        ddlItem.Items.Insert(0, new ListItem("-----全部-----", "0"));
    }

    protected void fv_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        SqlDataSource2.InsertParameters["Parent"].DefaultValue = ((DropDownList)fv.FindControl("ddlRegionID")).SelectedValue.ToString();
        SqlDataSource2.InsertParameters["RoadName"].DefaultValue = ((TextBox)fv.FindControl("txtRoadName")).Text.ToString();
        BindGV();
    }
}
